<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               v-model:page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template #menu-left>
        <el-button type="danger"
                   icon="el-icon-remove"
                   plain
                   @click="handleRemove">
          移 除
        </el-button>
      </template>
      <template #menu="{row,index,size}">
        <el-button text
                   type="primary"
                   icon="el-icon-remove"
                   @click.stop="handleRowRemove(row,index)">
          移 除
        </el-button>
      </template>
      <template #subscribeList-form="{}">
        <avue-crud :option="optionSubscribe" :data="dataSubscribe"/>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {getList, getDetail, remove} from "@/api/iot/mqtt/client";
import {mapGetters} from "vuex";
import functionEnum from "@/views/iot/component/functionStruct.vue";

export default {
  components: {functionEnum},
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 32,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        selection: true,
        labelWidth: 200,
        column: [
          {
            label: "客户端id",
            prop: "clientId",
            search: true,
            display: false,
            span: 24
          },
          {
            label: "用户名",
            prop: "username",
            search: true,
            display: false,
            span: 24,
          },
          {
            label: "ip",
            prop: "peerHost",
            display: false,
            span: 24,
            width: 200
          },
          {
            label: "心跳",
            prop: "keepalive",
            display: false,
            span: 24,
            width: 120
          },
          {
            label: "协议名称",
            prop: "protoName",
            display: false,
            span: 24,
            width: 120
          },
          {
            label: "协议版本",
            prop: "protoVer",
            display: false,
            span: 24,
            width: 120
          },
        ],
        group: [
          {
            label: '基础信息',
            prop: 'info',
            icon: 'el-icon-edit-outline',
            column: [
              {
                label: '客户端',
                prop: 'clientId'
              },
              {
                label: '用户名',
                prop: 'username'
              },
              {
                label: 'ip',
                prop: 'peerHost'
              },
              {
                label: '心跳',
                prop: 'keepalive'
              },
              {
                label: '协议名称',
                prop: 'protoName'
              },
              {
                label: '协议版本',
                prop: 'protoVer'
              },
            ]
          }, {
            label: '指标信息',
            prop: 'stat',
            icon: 'el-icon-view',
            column: [
              {
                label: '已发送字节数',
                prop: 'sentBytes'
              },
              {
                label: '已发送packet数',
                prop: 'sentPackets'
              },
              {
                label: '已处理字节数',
                prop: 'handledBytes'
              },
              {
                label: '已处理packet数',
                prop: 'handledPackets'
              },
              {
                label: '已接收字节数',
                prop: 'receivedBytes'
              },
              {
                label: '已接收packet数',
                prop: 'receivedPackets'
              },
              {
                label: '已接收TCP数据包次数',
                prop: 'receivedTcps'
              },
              {
                label: '处理packet平均耗时',
                prop: 'handledCostsPerPacket'
              },
              {
                label: '心跳超时次数',
                prop: 'heartbeatTimeoutCount'
              },
              {
                label: 'ChannelContext创建时间',
                prop: 'timeCreated'
              },
              {
                label: '首次连接成功的时间',
                prop: 'timeFirstConnected',
                span: 24
              },
            ]
          }, {
            label: '订阅列表',
            prop: 'subscribeList',
            icon: 'el-icon-edit-outline',
            column: [
              {
                label: '订阅详情',
                prop: 'subscribeList',
                span: 24
              },
            ]
          }
        ]
      },
      data: [],
      optionSubscribe: {
        header: false,
        size: 'small',
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        menuWidth: 80,
        column: [
          {
            label: "客户端",
            prop: "clientId",
            width: 250,
            span: 24
          },
          {
            label: "Topic",
            prop: "topicFilter",
            span: 24
          },
          {
            label: "QoS",
            prop: "mqttQoS",
            width: 80,
            span: 24
          },
        ]
      },
      dataSubscribe: [],
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.validData(this.permission.mqtt_client_add, false),
        viewBtn: this.validData(this.permission.mqtt_client_view, false),
        delBtn: this.validData(this.permission.mqtt_client_delete, false),
        editBtn: this.validData(this.permission.mqtt_client_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.clientId);
      });
      return ids.join(",");
    }
  },
  methods: {
    handleRowRemove(row) {
      this.$confirm("确定将选择客户端踢出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return remove(row.clientId);
      }).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "客户端踢出成功!"
        });
      });
    },
    handleRemove() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择客户端踢出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return remove(this.ids);
      }).then(() => {
        this.onLoad(this.page);
        this.$refs.crud.toggleSelection();
        this.$message({
          type: "success",
          message: "客户端踢出成功!"
        });
      });
    },
    beforeOpen(done, type) {
      if (["view"].includes(type)) {
        getDetail(this.form.clientId).then(res => {
          this.form = {
            ...res.data.data.info,
            ...res.data.data.stat,
          }
          this.dataSubscribe = res.data.data.subscribeList;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style>
</style>
